//
//  WHYVisitorView.m
//  WHYWeibo-OC
//
//  Created by 王洪运 on 15/11/23.
//  Copyright © 2015年 王洪运. All rights reserved.
//

#import "WHYVisitorView.h"
#import "UIButton+Extension.h"
#import "UIImageView+extension.h"
#import "UILabel+Extension.h"
#import <Masonry/Masonry.h>

@interface WHYVisitorView ()
@property (strong, nonatomic) UIImageView *iconView;
@property (strong, nonatomic) UIImageView *maskView;
@property (strong, nonatomic) UIImageView *homeView;
@property (strong, nonatomic) UILabel *messageLabel;
@property (strong, nonatomic) UIButton *registerButton;
@property (strong, nonatomic) UIButton *loginButton;

@property (copy, nonatomic) void(^clickedRegistorButtonBlock)();
@property (copy, nonatomic) void(^clickedLoginButtonBlock)();
@end

@implementation WHYVisitorView
#pragma mark - 懒加载
-(UIImageView *)iconView {
    if (_iconView == nil) {
        _iconView = [UIImageView imagwViewWithImageName:@"visitordiscover_feed_image_smallicon"];
    }
    return _iconView;
}

-(UIImageView *)maskView {
    if (_maskView == nil) {
        _maskView = [UIImageView imagwViewWithImageName:@"visitordiscover_feed_mask_smallicon"];
    }
    return _maskView;
}

-(UIImageView *)homeView {
    if (_homeView == nil) {
        _homeView = [UIImageView imagwViewWithImageName:@"visitordiscover_feed_image_house"];
    }
    return _homeView;
}

-(UILabel *)messageLabel {
    if (_messageLabel == nil) {
        _messageLabel = [UILabel labelWithTitle:@"关注一些人，回这里看看有什么惊喜" titleColor:[UIColor darkGrayColor] fontSize:14];
    }
    return _messageLabel;
}

-(UIButton *)registerButton {
    if (_registerButton == nil) {
        _registerButton = [UIButton buttonWithTitle:@"注册" titleColor:[UIColor orangeColor] fontSize:14 backImageName:@"common_button_white_disable"];
    }
    return _registerButton;
}

-(UIButton *)loginButton {
    if (_loginButton == nil) {
        _loginButton = [UIButton buttonWithTitle:@"登陆" titleColor:[UIColor darkGrayColor] fontSize:14 backImageName:@"common_button_white_disable"];
    }
    return _loginButton;
}

#pragma mark - 设置访客信息
-(void)setupInfoWithImageName:(NSString *)imageName title:(NSString *)title {
    self.messageLabel.text = title;
    
    if (imageName == nil) {
        [self startAnimation];
        return;
    }
    
    self.iconView.image = [UIImage imageNamed:imageName];
    
    self.homeView.hidden = YES;
    
    [self sendSubviewToBack:self.maskView];
    
}

#pragma mark - 设置首页访客界面的动画效果
-(void)startAnimation {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    animation.toValue = @(2 * M_PI);
    animation.repeatCount = MAXFLOAT;
    animation.duration = 20;
    animation.removedOnCompletion = NO;
    
    [self.iconView.layer addAnimation:animation forKey:nil];
}

#pragma mark - 重写构造方法
-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    [self setupUI];
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    [self setupUI];
    return self;
}

#pragma mark - 加载子控件
-(void)setupUI {
    [self addSubview:self.iconView];
    [self addSubview:self.maskView];
    [self addSubview:self.homeView];
    [self addSubview:self.messageLabel];
    [self addSubview:self.registerButton];
    [self addSubview:self.loginButton];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.centerY.mas_equalTo(self).offset(-60);
    }];
    
    [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self.registerButton);
    }];
    
    [self.homeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.iconView);
    }];
    
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.iconView.mas_bottom).offset(16);
        make.width.mas_equalTo(224);
    }];
    
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.messageLabel);
        make.top.mas_equalTo(self.messageLabel.mas_bottom).offset(16);
        make.size.mas_equalTo(CGSizeMake(100, 36));
    }];
    [self.registerButton addTarget:self action:@selector(clickRegistorButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.messageLabel);
        make.centerY.mas_equalTo(self.registerButton);
        make.size.mas_equalTo(self.registerButton);
    }];
    [self.loginButton addTarget:self action:@selector(clickLoginButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.backgroundColor = [UIColor colorWithWhite:237.0 / 255.0 alpha:1.0];
    
}

#pragma mark - 监听按钮点击事件
-(void)clickRegistorButton {
    if (self.clickedRegistorButtonBlock) {
        self.clickedRegistorButtonBlock();
    }
}

-(void)clickLoginButton {
    if (self.clickedLoginButtonBlock) {
        self.clickedLoginButtonBlock();
    }
}

-(void)visitorViewDidClickRegisterButton:(WHYButtonClickBock)clickedBlock {
    if (clickedBlock) {
        self.clickedRegistorButtonBlock = clickedBlock;
    }
}

-(void)visitorViewDidClickLoginButton:(WHYButtonClickBock)clickedBlock {
    if (clickedBlock) {
        self.clickedLoginButtonBlock = clickedBlock;
    }
}


@end
